/*
 * Copyright (c) 2011 Metrum Research Group LLC. All Rights Reserved.
 *
 * You may use this software under the terms of GPL v3 (http://www.gnu.org/licenses/gpl.html).
 *
 * METRUM RESEARCH GROUP LLC MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
 * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. METRUM RESEARCH GROUP LLC SHALL NOT BE LIABLE FOR
 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 */

package sumo;

import java.util.List;
import java.util.Iterator;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class UserCommandServlet extends HttpServlet { 

	private static final int MAX_LEN = 500;
	
	/**
	 * Need a UID to extend HttpServlet
	 */
	private static final long serialVersionUID = -2985631145912953308L;

	public void doPost(HttpServletRequest req, HttpServletResponse res) 
		throws ServletException, IOException{ 
		// authorize
		HttpSession theSession = req.getSession();
                String username = (String)theSession.getAttribute("username");
                if(username == null) return;
                // Get the user input
		String expr = req.getParameter("theUserInput");
		if(expr != null){
			OutputStream os = (OutputStream) theSession.getAttribute("SESSION_OUTPUT_STREAM");
			if(expr.trim().length() == 0){
				// Push in a NEWLINE
		                os.write(System.getProperty("line.separator").getBytes());
                		os.flush();
			}else{
                		//os.flush();
                		ExpressionEvaluator R = (ExpressionEvaluator) theSession.getAttribute("evaluator");
                		String result = R.evaluate(expr);
			}
		}
	}
}




